#include "UnitTest.h"


static Thread::Locker sLocker;

static u32 __stdcall ThreadProc( void *arg )
{
    Thread::ScopeLocker scopeLocker( &sLocker );

    printf( "%d beigin\n", arg );
    Sleep( 100 );
    printf( "%d end\n\n", arg );

    return 0;
}

DCL_UNITTEST( TestMultiThread )
{
    const u32 cThreadNum = 5;
    Thread threads[cThreadNum];
    
    for ( u32 i = 0; i < cThreadNum; ++i )
    {
        char name[256];
        sprintf_s( name, 256, "TestMultiThread - %d", i );

        threads[i].Create( ThreadProc, (void *)i, name );
    }
}


static Thread::Safer<Timer> sTimer;

static u32 __stdcall ThreadProcB( void *arg )
{
    u32 currTime = 0;
    while ( currTime < 1000 )
    {
        sTimer.Lock();
        currTime = sTimer->GetTime();
        sTimer->SetTime( currTime );
        currTime = sTimer->GetTime();
        ASSERT( currTime < 10000, "Time value invalid" );
        sTimer.UnLock();
    }

    return 0;
}

DCL_UNITTEST( TestThreadSafer )
{
    sTimer->Start();

    const u32 cThreadNum = 10;
    Thread threads[cThreadNum];

    for ( u32 i = 0; i < cThreadNum; ++i )
    {
        char name[256];
        sprintf_s( name, 256, "TestThreadSafer - %d", i );

        threads[i].Create( ThreadProcB, NULL, name );
    }
}